package com.zj;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ZjBlockingQueue<T> {

    private Deque<T> deque=new ArrayDeque<>();

    private ReentrantLock lock=new ReentrantLock();

    private Condition fullWait=lock.newCondition();

    private Condition emptyWait=lock.newCondition();

    private int capcity;

    public ZjBlockingQueue(int capcity) {
        this.capcity = capcity;
    }

    //阻塞获取
    public T take(){
        lock.lock();
        try{
            while(deque.isEmpty()){
                try {
                    emptyWait.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            T t=deque.removeFirst();
            fullWait.signal();
            return t;
        }finally {
            lock.unlock();
        }
    }
    //带超时的获取
    public T poll(long timeout, TimeUnit unit){
        lock.lock();
        try{
            long nanos = unit.toNanos(timeout);
            while(deque.isEmpty()){
                try {
                    if(nanos<=0){
                        return null;
                    }
                    nanos = emptyWait.awaitNanos(nanos);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            T t=deque.removeFirst();
            fullWait.signal();
            return t;
        }finally {
            lock.unlock();
        }
    }

    public void put(T element){
        lock.lock();
        try{
            while(deque.size()>=capcity){
                try {
                    System.out.println(element+" deque fullWait!");
                    fullWait.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            deque.addLast(element);
            System.out.println("deque add "+element);
            emptyWait.signal();
        }finally {
            lock.unlock();
        }
    }

    boolean offer(T element,long timeout,TimeUnit unit){
        lock.lock();
        try{
            long nanos = unit.toNanos(timeout);
            while(deque.size()>=capcity){
                try {
                    System.out.println(element+" deque fullWait!");
                    nanos=fullWait.awaitNanos(nanos);
                    if(nanos<=0){
                        return false;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            deque.addLast(element);
            System.out.println("deque add "+element);
            emptyWait.signal();
            return true;
        }finally {
            lock.unlock();
        }
    }
    public int size(){
        lock.lock();
        try{
            return deque.size();
        }finally {
            lock.unlock();
        }
    }



}
